package com.wash.shoes.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wash.shoes.domain.vo.ShoeRackGridVo;
import com.wash.shoes.entity.ShoeRackGridPo;
import com.wash.shoes.entity.ShoeRackPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.mapper.ShoeRackGridMapper;
import com.wash.shoes.mapper.ShoeRackMapper;
import com.wash.shoes.mapper.ShopShoeRackMapper;
import com.wash.shoes.page.PageFactory;
import com.wash.shoes.service.ShoeRackGridService;
import com.wash.shoes.util.GridShareUtil;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 鞋架的格子 服务实现类
 * </p>
 *
 * @author ljg
 * @since 2024-08-27
 */
@Service
public class ShoeRackGridServiceImpl extends ServiceImpl<ShoeRackGridMapper, ShoeRackGridPo> implements ShoeRackGridService {


    @Resource
    ShoeRackMapper shoeRackMapper;

    @Resource
    ShopShoeRackMapper shopShoeRackMapper;


    @PostConstruct
    public void init() {

        //   shareGridList(5, "202411136998506129");
    }

    @Override
    public List<ShoeRackGridPo> shareGridList(Integer shoeNum, String shopId) {
        // 先考虑一个货架能分配的情况
        List<String> rackIds = this.baseMapper.getCanUserRack(shoeNum);
        // 门店所有的鞋架
        List<String> shoeRackList = shopShoeRackMapper.shopAllRackData(shopId);
        if (shoeRackList.isEmpty()) {
            throw new WashException("该店铺未分配鞋架");
        }
        // rackIds 保留 shoeRackList存在的数据
        rackIds.retainAll(shoeRackList);
        // 查询所有的数据
        if (rackIds.isEmpty()) {
            throw new WashException("店铺鞋架已经用完,请仔细检查鞋架使用情况");
        }
        List<ShoeRackPo> allGridList = this.shoeRackMapper.selectBatchIds(rackIds);
        // 排序 allGridList
        allGridList.sort(Comparator.comparingInt(o -> Integer.parseInt(o.getRackCode())));

        // 没有货架可以分配的情况
        if (rackIds.isEmpty()) {
            return new ArrayList<>();
        } else {
            // 从大到小获取空闲的，如果相邻的分配
            for (ShoeRackPo shoeRackPo : allGridList) {
                // 查询所有的格子
                List<ShoeRackGridPo> shoeRackGridPoList = this.baseMapper.getEmptyRack(shoeRackPo.getId(), 1000);
                List<ShoeRackGridPo> sharePoList = GridShareUtil.shareGridContinuous(shoeRackGridPoList, shoeNum);
                if (sharePoList.size() == shoeNum) {
                    return sharePoList;
                }
            }
            // 这里是同一个货架分配
            for (ShoeRackPo shoeRackPo : allGridList) {
                List<ShoeRackGridPo> shoeRackGridPoList = this.baseMapper.getEmptyRack(shoeRackPo.getId(), shoeNum);
                if (shoeRackGridPoList.size() == shoeNum) {
                    return shoeRackGridPoList;
                }
            }

        }
        return new ArrayList<>();
    }

    @Override
    public Page<ShoeRackGridVo> pageList(ShoeRackGridPo shoeRackGridPo) {
        Page page = PageFactory.defaultPage();
        List<ShoeRackGridVo> dataList = baseMapper.pageList(page, shoeRackGridPo);
        page.setRecords(dataList);
        return page;
    }

    @Override
    public ShoeRackGridPo getByShoeId(String shoeId) {
        return this.baseMapper.getByShoeId(shoeId);
    }


    @Override
    public Integer clearGridShoe(String rackGridId) {
        return this.baseMapper.clearGridShoe(rackGridId);
    }

    @Override
    public List<ShoeRackGridPo> getRackGrid(String theRackId) {
        return this.baseMapper.getRackGrid(theRackId);
    }


}
